package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.order.pojo.Evaluate;
import com.changgou.search.dao.ItemMapper;
import com.changgou.search.service.ItemService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<Evaluate> findAllById() {
        String evaluateList = stringRedisTemplate.opsForValue().get("evaluateList");
        List<Evaluate> list = JSON.parseArray(evaluateList, Evaluate.class);
        return list;
    }


    @Scheduled(cron = "0/20 * * * * ?")
    public void findAll(){
        List<Evaluate> evaluateList = itemMapper.selectAll();
        for (Evaluate evaluate : evaluateList) {
            String spec = evaluate.getSpec();
            List<String> specList=new ArrayList<>();
            Map<String, String> map = JSON.parseObject(spec, Map.class);
            if (map != null) {
                for (String s : map.keySet()) {
                    System.out.println(map.get(s));
                    specList.add(map.get(s));
                    System.out.println(map);
                }
            }
            evaluate.setSpecList(specList);
        }
        stringRedisTemplate.opsForValue().set("evaluateList",JSON.toJSONString(evaluateList));
    }

}
